<template>
  <div class="register-page">
    <div class="register-container">
      <div class="register-content">
        <!-- 左侧 Logo 和介绍 -->
        <div class="register-banner">
          <div class="banner-content">
            <h1>广告营销管理系统</h1>
            <p class="banner-desc">加入我们 · 开启智能营销之旅</p>
          </div>
        </div>

        <!-- 右侧注册表单 -->
        <div class="register-form">
          <div class="form-header">
            <h2>账号注册</h2>
            <p class="welcome-text">欢迎加入，请创建您的账号</p>
          </div>

          <form @submit.prevent="submitForm">
            <div class="form-group">
              <el-input
                v-model="username"
                placeholder="请输入用户名"
                prefix-icon="User"
                :disabled="loading"
              />
            </div>

            <div class="form-group">
              <el-input
                v-model="password"
                type="password"
                placeholder="请输入密码"
                prefix-icon="Lock"
                show-password
                :disabled="loading"
              />
            </div>

            <div class="form-group">
              <el-input
                v-model="confirmPassword"
                type="password"
                placeholder="请确认密码"
                prefix-icon="Lock"
                show-password
                :disabled="loading"
              />
            </div>

            <!-- 身份选择 -->
            <div class="form-group">
              <el-radio-group v-model="role" class="role-radio-group">
                <el-radio label="advertiser">广告用户</el-radio>
                <el-radio label="device_owner">设备拥有机构</el-radio>
              </el-radio-group>
            </div>

            <el-button
              type="primary"
              class="submit-btn"
              :loading="loading"
              @click="submitForm"
            >
              {{ loading ? '注册中...' : '立即注册' }}
            </el-button>

            <div class="form-footer">
              <span>已有账号？</span>
              <router-link to="/login" class="login-link">立即登录</router-link>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { register } from '@/api/user'

const router = useRouter()
const username = ref('')
const password = ref('')
const confirmPassword = ref('')
const role = ref('') // 新增：身份选择
const loading = ref(false)

const validateForm = () => {
  if (!username.value) {
    ElMessage.warning('请输入用户名')
    return false
  }
  if (!password.value) {
    ElMessage.warning('请输入密码')
    return false
  }
  if (!confirmPassword.value) {
    ElMessage.warning('请确认密码')
    return false
  }
  if (password.value !== confirmPassword.value) {
    ElMessage.error('两次输入的密码不一致')
    return false
  }
  if (password.value.length < 6) {
    ElMessage.warning('密码长度不能少于6位')
    return false
  }
  if (!role.value) {
    ElMessage.warning('请选择身份')
    return false
  }
  return true
}

const submitForm = async () => {
  if (!validateForm()) return

  try {
    loading.value = true
    const { data } = await register({
      username: username.value,
      password: password.value,
      confirmPassword: confirmPassword.value,
      role: role.value // 新增：传递身份信息
    })

    if (data.code === 200) {
      ElMessage.success('注册成功')
      router.push('/login')
    } else {
      ElMessage.error(data.message || '注册失败')
    }
  } catch (error) {
    console.error('注册失败:', error)
    ElMessage.error('注册请求失败，请检查网络')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.register-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #1890ff 0%, #1d39c4 100%);
  padding: 20px;
}

.register-container {
  width: 1000px;
  max-width: 90%;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.register-content {
  display: flex;
  min-height: 600px;
}

.register-banner {
  flex: 1;
  background: linear-gradient(135deg, #1890ff 0%, #1d39c4 100%);
  padding: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  position: relative;
  overflow: hidden;
}

.banner-content {
  position: relative;
  z-index: 1;
  text-align: center;
}

.banner-content h1 {
  font-size: 2.5em;
  margin-bottom: 20px;
  font-weight: 600;
}

.banner-desc {
  font-size: 1.1em;
  opacity: 0.9;
  line-height: 1.6;
}

.register-form {
  flex: 1;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
}

.form-header h2 {
  font-size: 28px;
  color: #333;
  margin-bottom: 10px;
}

.welcome-text {
  color: #666;
  font-size: 16px;
}

.form-group {
  margin-bottom: 24px;
}

.form-group :deep(.el-input__wrapper) {
  padding: 12px;
  box-shadow: 0 0 0 1px #dcdfe6;
}

.form-group :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #1890ff;
}

.role-radio-group {
  width: 100%;
}

.submit-btn {
  width: 100%;
  padding: 12px;
  font-size: 16px;
  margin-bottom: 24px;
}

.form-footer {
  text-align: center;
  color: #666;
  font-size: 14px;
}

.login-link {
  color: #1890ff;
  text-decoration: none;
  margin-left: 8px;
}

.login-link:hover {
  color: #40a9ff;
}

@media (max-width: 768px) {
  .register-content {
    flex-direction: column;
  }

  .register-banner {
    padding: 30px;
    min-height: 200px;
  }

  .banner-content h1 {
    font-size: 2em;
  }

  .register-form {
    padding: 30px;
  }
}
</style>